const allServices = require('../../controller/mysql')

const search = {
    search: (req, res) => {
        const key = req.query.key;
        const sql = `SELECT shop_id, shop_name, shop_dec, shop_price, shop_img FROM shoplist WHERE shop_name LIKE ?`;
        allServices.query(sql, [`%${key}%`])
            .then((result) => {
                res.status(200).json(result);
            })
            .catch((error) => console.error(error));
    },
    search_orderPrice: (req, res) => {
        const sql = `SELECT shop_id, shop_name, shop_dec, shop_price, shop_img
             FROM shoplist
             WHERE shop_name LIKE ?
             ORDER BY shop_price DESC`;
        const keyword = `%${req.query.key}%`;
        allServices.query(sql, [keyword])
            .then((result) => {
                res.status(200).json({
                    code: 200,
                    msg: "请求成功",
                    res: result
                })
            })
            .catch((error) => console.error(error));
    }
}

module.exports = search